package adminConfigService

import (
	"encoding/json"

	"gitee.com/krio/ginc/example/core/exerr"
	exampleDtoApiPbAdminConfig "gitee.com/krio/ginc/example/dto/api/pb/admin/config"
	adminConfigDao "gitee.com/krio/ginc/example/internal/admin/config/dao"
	adminConfigModel "gitee.com/krio/ginc/example/internal/admin/config/model"
	commonEntry "gitee.com/krio/ginc/example/internal/common/entry/common"
	commonModel "gitee.com/krio/ginc/example/internal/common/model/common"
	"github.com/gin-gonic/gin"
)

// AdminConfigList 列表
func AdminConfigList(ctx *gin.Context, req *exampleDtoApiPbAdminConfig.AdminConfigListReq) (*exampleDtoApiPbAdminConfig.AdminConfigListResp, error) {
	resp := &exampleDtoApiPbAdminConfig.AdminConfigListResp{}
	search := struct {
		Keyword string `json:"keyword"`
	}{}
	if req.Keywords != "" {
		err := json.Unmarshal([]byte(req.Keywords), &search)
		if err != nil {
			return nil, err
		}
	}
	dao := &adminConfigDao.AdminConfigDao{}
	list, total, err := dao.ConfigList(ctx, &commonEntry.ListCommonReq{
		Page:    req.Page,
		Limit:   req.Limit,
		Keyword: search.Keyword,
		OrderBy: []*commonEntry.OrderBy{
			{
				IsDesc: true,
				Field:  "id",
			},
		},
	})
	if err != nil {
		return nil, err
	}
	var data []*exampleDtoApiPbAdminConfig.AdminConfigInfo
	for _, v := range list {
		data = append(data, formatDetail(v))
	}
	resp.Total = total
	resp.List = data
	return resp, nil
}

// AdminConfigDetail 详情
func AdminConfigDetail(ctx *gin.Context, req *exampleDtoApiPbAdminConfig.AdminConfigDetailReq) (*exampleDtoApiPbAdminConfig.AdminConfigInfo, error) {
	dao := &adminConfigDao.AdminConfigDao{}
	detail, has, err := dao.ConfigDetail(ctx, req.Id)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, exerr.DataNotFound
	}

	return formatDetail(detail), nil
}

// AdminConfigAdd 新增
func AdminConfigAdd(ctx *gin.Context, req *exampleDtoApiPbAdminConfig.AdminConfigInfo) (*exampleDtoApiPbAdminConfig.AdminConfigAddResp, error) {
	one := &adminConfigModel.AdminConfig{
		Code:     req.Code,
		Title:    req.Title,
		Remark:   req.Remark,
		MetaData: nil,
		Status:   commonModel.DataStatus(req.Status),
	}
	err := json.Unmarshal([]byte(req.MetaData), &one.MetaData)
	if err != nil {
		return nil, err
	}
	dao := &adminConfigDao.AdminConfigDao{}
	id, err := dao.ConfigAdd(ctx, one)
	if err != nil {
		return nil, err
	}
	return &exampleDtoApiPbAdminConfig.AdminConfigAddResp{
		Id: id,
	}, nil
}

// AdminConfigUpdate 修改
func AdminConfigUpdate(ctx *gin.Context, req *exampleDtoApiPbAdminConfig.AdminConfigInfo) (*exampleDtoApiPbAdminConfig.AdminConfigAddResp, error) {
	one := &adminConfigModel.AdminConfig{
		Basic:  commonModel.Basic{ID: req.Id},
		Code:   req.Code,
		Title:  req.Title,
		Remark: req.Remark,
		Status: commonModel.DataStatus(req.Status),
	}
	err := json.Unmarshal([]byte(req.MetaData), &one.MetaData)
	if err != nil {
		return nil, err
	}
	dao := &adminConfigDao.AdminConfigDao{}
	_, err = dao.ConfigUpdate(ctx, one)
	if err != nil {
		return nil, err
	}
	return &exampleDtoApiPbAdminConfig.AdminConfigAddResp{}, nil
}

// AdminConfigDelete 删除
func AdminConfigDelete(ctx *gin.Context, req *exampleDtoApiPbAdminConfig.AdminConfigDeleteReq) (*exampleDtoApiPbAdminConfig.AdminConfigDeleteResp, error) {
	dao := &adminConfigDao.AdminConfigDao{}
	err := dao.ConfigDelete(ctx, req.Ids)
	if err != nil {
		return nil, err
	}
	return &exampleDtoApiPbAdminConfig.AdminConfigDeleteResp{}, nil
}

// 数据装换
func formatDetail(v *adminConfigModel.AdminConfig) *exampleDtoApiPbAdminConfig.AdminConfigInfo {
	bt, _ := json.Marshal(v.MetaData)
	return &exampleDtoApiPbAdminConfig.AdminConfigInfo{
		Id:         v.ID,
		Code:       v.Code,
		Title:      v.Title,
		Remark:     v.Remark,
		MetaData:   string(bt),
		Status:     int32(v.Status),
		CreateTime: v.CreatedAt.Format("2006-01-02 15:04:05"),
		UpdateTime: v.UpdatedAt.Format("2006-01-02 15:04:05"),
	}
}
